import {
  get,
  post,
  postDWG
} from './https'
import {
  formatTime
} from './tools'
import mock from './../components/SearchAndTable/mock'
export {
  formatTime
}
// 获取图片url
export const getFileUrl = file => {
  let getUrl = null
  if (window.createObjectURL !== undefined) { // basic
    getUrl = window.createObjectURL(file)
  } else if (window.URL !== undefined) { // mozilla(firefox)
    getUrl = window.URL.createObjectURL(file)
  } else if (window.webkitURL !== undefined) { // webkit or chrome
    getUrl = window.webkitURL.createObjectURL(file)
  }
  return getUrl
}
export let base64Prefix = 'data:image/png;base64,' + ''
// 图片base64转文件
export const base64ImgtoFile = (dataurl = base64Prefix, filename = 'img' + Date.parse(new Date())) => {
  let base64 = dataurl.split(',')[1]
  if (base64) {
    base64 = base64.replace(/-/g, '+')
  }
  if (base64) {
    base64 = base64.replace(/_/g, '/')
  }
  let bstr = window.atob(base64)
  bstr = decodeURIComponent(escape(bstr))
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], `${filename}.${'upload.jpeg'}`, {
    type: 'image/jpeg',
    lastModified: Date.now()
  })
}

export let isDevelop = /^192\.168\..*8080$/.test(location.host)
export let baseUrl = '/api' // proxy 'config/index.js'

/**
 * 获取tableData
 */
export const getTableData = (param) => {
//   let {
//     page,
//     size,
//     name = '',
//     phone = ''
//   } = param
  // return get(`${baseUrl}/qr-code/api/getList/${page}/${size}?name=${name}&phone=${phone}`)
  return mock.getTableData
}

/**
 * 新增内容
 */
export const newReply = (param) => {
  return post(`${baseUrl}/shenzhen/userRelevant/addPartyReplay`, param)
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  let userInfo = sessionStorage.getItem('userInfo')
  return JSON.parse(userInfo)
}

/**
 * 登录
 */
export const login = (param) => {
  return post(`${baseUrl}/login`, param)
}
/**
 * 用户列表
 */
export const userList = (param) => {
  return post(`${baseUrl}/user/list`, param)
}
/**
 * 用户列表
 * 新增
 */
export const userAdd = (param) => {
  let createUser = getUserInfo().id
  return post(`${baseUrl}/user/add`, {
    ...param,
    createUser
  })
}
/**
 * 用户列表
 * 删除
 */
export const userDel = (param) => {
  return post(`${baseUrl}/user/delete`, param)
}
/**
 * 对比表格
 * 展示
 */
export const contrastList = (param) => {
  return post(`${baseUrl}/contrast/list`, param)
}
/**
 * 对比表格
 * 删除
 * contrast
 */
export const contrastDel = (param) => {
  return post(`${baseUrl}/contrast/delete`, param)
}
/**
 * 对比表格
 * 新增
 */
export const contrastAdd = (param) => {
  let createUser = getUserInfo().id
  return post(`${baseUrl}/contrast/add`, {
    ...param,
    createUser
  })
}
export const downImg = async (param) => {
  let {
    data
  } = await post(`${baseUrl}/file/download`, param)
  return 'data:image/png;base64,' + data
}
/**
 * 上传klm
 */
export const uploadKlm = (param) => {
  return postDWG(`${baseUrl}/upload/uploadAction`, param)
}
/**
 * 上传图片
 */
export const uploadImg = (file) => {
  let param = new FormData()
  param.append('file', file)
  return post(`${baseUrl}/file/uploadFile`, param)
}
/**
 * 对比表格
 * 展示
 */
export const historyList = (param) => {
  return post(`${baseUrl}/history/list`, param)
}
/**
 * 图1
 */
export const netWoekServerDwg = (param) => {
  return get(`${baseUrl}/netWorkSystem/uploadAction?params="${param}"`)
}
